<?php

namespace app\bbs\controller;

use think\Controller;
use think\Db;
use think\Session;

class PublishArticles extends Controller
{
    public function index()
    {
        $user_info = Session::get('bbs_user_info');
        if ($user_info) {
            $ReleaseAlias = input('ReleaseAlias', null);
            if ($ReleaseAlias) {
                $head = head(null, null, 'layui-this');

                $bbs_classification_son = Db::table('bbs_classification_son')
                    ->where([
                        'son_class_alias' => $ReleaseAlias,
                        'son_class_release' => 0,
                    ])
                    ->field('id,son_class_name')
                    ->find();
                if (!$bbs_classification_son) {
                    $Data['code'] = '10002';
                    $Data['ErrorPosition'] = 'PublishArticles';
                    $Data['ErrorMsg'] = '禁止发布';
                    return json($Data);
                    return;
                }
                $bbs_article_class_list = Db::table('bbs_article_class') // 分类 及设置
                    ->where("bbs_classification_son_id = $bbs_classification_son[id]")
                    ->select();

                $web_settings = Db::table('bbs_settings')
                    ->where(['id' => 1])
                    ->find();

                $RandStr = randstr(128); //生成一个128位的字符串
                Session::set('bbs_PublishArticles_RandStr', $RandStr);
                Session::set('bbs_PublishArticles_RandStr_success_time', time()); //生成随机数的时间

                $bbs_jurisdiction_level = Db::table('bbs_jurisdiction_level')
                    ->select();

                return $this->fetch('publish_articles', ['head' => $head, 'web_settings' => $web_settings, 'bbs_article_class_list' => $bbs_article_class_list, 'bbs_classification_son' => $bbs_classification_son, 'RandStr' => $RandStr, 'ReleaseAlias' => $ReleaseAlias, 'bbs_jurisdiction_level' => $bbs_jurisdiction_level]);
            } else {
                $Data['code'] = '10000';
                $Data['ErrorPosition'] = 'PublishArticles';
                $Data['ErrorMsg'] = '没有传入别名';
                return json($Data);
            }
        } else {
            $Data['code'] = '10001';
            $Data['ErrorPosition'] = 'PublishArticles';
            $Data['ErrorMsg'] = '用户未登录';
            return json($Data);
        }
    }
    public function release()
    {
        $user_info = Session::get('bbs_user_info');
        $bbs_user_info_count = Db::table('user')->where($user_info)->count();
        $ReleaseAlias = input('ReleaseAlias', null);
        if ($ReleaseAlias) {
            $bbs_classification_son = Db::table('bbs_classification_son')
                ->alias('bbs_classification_son')
                ->where("bbs_classification_son.son_class_alias='$ReleaseAlias'")
                ->field('son_class_release,id')
                ->find();
            if ($bbs_classification_son['son_class_release'] == 1) {
                $Data['code'] = '10002';
                $Data['ErrorPosition'] = 'PublishArticles';
                $Data['ErrorMsg'] = '禁止发布';
                return json($Data);
            }
        } else {
            $Data['code'] = '10000';
            $Data['ErrorPosition'] = 'PublishArticles';
            $Data['ErrorMsg'] = '没有传入别名';
            return json($Data);
        }
        if ($bbs_user_info_count == 1) {
            $bbs_settings = Db::table('bbs_settings') // 获取网站设置
                ->where('id=1')
                ->field('rand_str_out_time')
                ->find();
            $L_title = input('L_title', null);
            $class = input('class', null);

            $bbs_article_class = Db::table('bbs_article_class')
                ->where(['id' => $class])
                ->find();
            if ($bbs_article_class) {
                if ($bbs_article_class['to_examine'] == 1) {
                    $start = 1; //审核
                }else{
                    $start = 4; //审核
                }

                $textarea = input('textarea', null);
                $RandStr = input('RandStr', null);
                $jurisdiction_number = rsa_decode(input('jurisdiction_number', null)); // 用户阅读权限的权限数值
                $jurisdiction_name = input('jurisdiction_name', null); // 用户阅读权限的权限名字
                $jurisdiction_info = Db::table('bbs_jurisdiction_level')
                    ->where([
                        'level_name' => $jurisdiction_name,
                        'id' => $jurisdiction_number,
                    ])
                    ->find();
                $bbs_PublishArticles_RandStr = Session::get('bbs_PublishArticles_RandStr');
                $bbs_PublishArticles_RandStr_success_time = Session::get('bbs_PublishArticles_RandStr_success_time'); //生成随机数的时间
                $comment_email_notification = input('comment_email_notification', 0);
                if ($comment_email_notification > 1) { //1开启 0 关闭
                    $comment_email_notification = 0;
                }
                if ($L_title && $class && $textarea && $RandStr && $RandStr == $bbs_PublishArticles_RandStr && time() <  $bbs_PublishArticles_RandStr_success_time +  $bbs_settings['rand_str_out_time'] && $jurisdiction_number && $jurisdiction_name && $jurisdiction_info) {
                    $textarea = str_replace('<script>', '&lt;script&gt;', $textarea);
                    Db::table('bbs_article')
                        ->insert([
                            'title' => $L_title,
                            'content' => $textarea,
                            'class_id' => $bbs_classification_son['id'],
                            'user_id' => $user_info['id'],
                            'release_time' => date('Y-m-d H:i:s'),
                            'top' => 0,
                            'top_time' => 0,
                            'release_data' => date('Y-m-d'),
                            'Classification_main' => $class,
                            'time_stamp' => time(),
                            'comment_email_notification' => $comment_email_notification,
                            'are_comments_allowed' => 1,
                            'start' => $start,
                            'permission_to_read' => $jurisdiction_info['jurisdiction_number'],
                        ]);
                } else {
                    if (!$L_title) {
                        $Data['code'] = '10003';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '请输入标题';
                        return json($Data);
                    }
                    if (!$class) {
                        $Data['code'] = '10004';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '请选择分类';
                        return json($Data);
                    }
                    if (!$textarea) {
                        $Data['code'] = '10005';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '请输入内容';
                        return json($Data);
                    }
                    if (!$RandStr) {
                        $Data['code'] = '10006';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '没有发现验证字符串';
                        return json($Data);
                    }
                    if ($RandStr != $bbs_PublishArticles_RandStr) {
                        $Data['code'] = '10007';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = $RandStr;
                        $Data['ErrorMsg2'] = $bbs_PublishArticles_RandStr;
                        return json($Data);
                    }
                    if (time() >  $bbs_PublishArticles_RandStr_success_time +  $bbs_settings['rand_str_out_time']) {
                        $Data['code'] = '10008';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '验证字符串过期';
                        return json($Data);
                    }
                    if (!$jurisdiction_number) {
                        $Data['code'] = '10009';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '缺少阅读权限 - number';
                        return json($Data);
                    }
                    if (!$jurisdiction_name) {
                        $Data['code'] = '10010';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '缺少阅读权限 - name';
                        return json($Data);
                    }
                    if (!$jurisdiction_info) {
                        $Data['code'] = '10011';
                        $Data['ErrorPosition'] = 'PublishArticles';
                        $Data['ErrorMsg'] = '根据传入的阅读权限信息没有找到';
                        return json($Data);
                    }
                }
            } else {
                $Data['code'] = '10004';
                $Data['ErrorPosition'] = 'PublishArticles';
                $Data['ErrorMsg'] = '请选择分类';
                return json($Data);
            }
        } else {
            $Data['code'] = '10001';
            $Data['ErrorPosition'] = 'PublishArticles';
            $Data['ErrorMsg'] = '用户未登录';
            return json($Data);
        }
    }
}
